package com.codedancer.campus.po;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.codedancer.campus.common.typehandler.CodeEnumTypeHandler;
import com.codedancer.campus.enums.UserSourceType;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.gitee.sunchenbin.mybatis.actable.annotation.Column;
import com.gitee.sunchenbin.mybatis.actable.annotation.Table;
import com.gitee.sunchenbin.mybatis.actable.annotation.Unique;
import com.gitee.sunchenbin.mybatis.actable.constants.MySqlTypeConstant;
import java.io.Serializable;
import java.util.Date;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@TableName(value = "user", autoResultMap = true)
@Table(name = "user", comment = "用户")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class User extends Model<User> implements Serializable {

    @TableId
    @TableField("uuid")
    @Column(name = "uuid", type = MySqlTypeConstant.VARCHAR, length = 36, isKey = true, isNull = false, comment = "用户uuid")
    private String uuid;

    @TableField("nick_name")
    @Column(name = "nick_name", type = MySqlTypeConstant.VARCHAR, length = 8, isNull = true, comment = "用户昵称")
    @Unique
    private String nickName;

    @TableField("password")
    @Column(name = "password", type = MySqlTypeConstant.VARCHAR, length = 64, isNull = false, comment = "密码")
    private String password;

    @TableField("salt")
    @Column(name = "salt", type = MySqlTypeConstant.VARCHAR, length = 64, isNull = false, comment = "盐值")
    private String salt;

    @TableField("disabled")
    @Column(name = "disabled", type = MySqlTypeConstant.BIT, isNull = false, comment = "是否禁用", defaultValue = "false")
    private Boolean disabled;

    @TableField("avatar")
    @Column(name = "avatar", type = MySqlTypeConstant.VARCHAR, length = 128, isNull = true, comment = "用户头像")
    private String avatar;

    @TableField("signature")
    @Column(name = "signature", type = MySqlTypeConstant.VARCHAR, length = 50, isNull = true, comment = "个性签名")
    private String signature;

    @TableField("sex")
    @Column(name = "sex", type = MySqlTypeConstant.INT, length = 1, isNull = true, comment = "性别 1表示男 2表示女 3表示未知")
    private Integer sex;

    @TableField("deleted")
    @Column(name = "deleted", type = MySqlTypeConstant.TINYINT, length = 1, isNull = false, comment = "是否删除 1表示删除 0表示未删除", defaultValue = "0")
    private Short deleted;

    @TableField(value = "source_type", typeHandler = CodeEnumTypeHandler.class)
    @Column(name = "source_type", type = MySqlTypeConstant.TINYINT, length = 1, isNull = false, comment = "用户来源类型 1表示注册 2表示手机 3表示QQ 4表示微信")
    private UserSourceType sourceType;

    @Column(name = "create_time", type = MySqlTypeConstant.DATETIME, isNull = false, comment = "创建时间")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    @Column(name = "update_time", type = MySqlTypeConstant.DATETIME, isNull = false, comment = "更新时间")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private Date updateTime;

    @TableField("phone")
    @Column(name = "phone", type = MySqlTypeConstant.VARCHAR, length = 11, isNull = false, comment = "手机号", defaultValue = "")
    private String phone;

    @TableField("is_set_basic_message")
    @Column(name = "is_set_basic_message", type = MySqlTypeConstant.BIT, isNull = false, comment = "是否设置了基本信息", defaultValue = "false")
    private Boolean isSetBasicMessage;

    @TableField("birthday")
    @Column(name = "birthday", type = MySqlTypeConstant.VARCHAR, length = 10, isNull = true, comment = "生日")
    private String birthday;

    @TableField("background_image")
    @Column(name = "background_image", type = MySqlTypeConstant.VARCHAR, length = 128, isNull = true, comment = "背景图片")
    private String backgroundImage;

    @TableField("ip_address")
    @Column(name = "ip_address", type = MySqlTypeConstant.VARCHAR, length = 10, isNull = true, comment = "ip地址")
    private String ipaddress;

    @TableField("violation_count")
    @Column(name = "violation_count", type = MySqlTypeConstant.INT, length = 1, isNull = false, comment = "违规次数，不能超过3次", defaultValue = "0")
    private Integer violationCount;

    // 所属学校
    @TableField(exist = false)
    private School school;

    /**
     * 是否超过违规次数
     *
     * @return
     */
    public boolean isExceedViolationCount() {
        return violationCount >= 3;
    }
}
